/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.ranger.authorization.hadoop;

import org.apache.hadoop.hdfs.server.namenode.INodeAttributeProvider;
import org.apache.hadoop.hdfs.server.namenode.INodeAttributes;
import org.apache.ranger.plugin.classloader.RangerPluginClassLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RangerHdfsAuthorizer extends INodeAttributeProvider {
    private static final Logger LOG = LoggerFactory.getLogger(RangerHdfsAuthorizer.class);

    private static final String RANGER_PLUGIN_TYPE                    = "hdfs";
    private static final String RANGER_HDFS_AUTHORIZER_IMPL_CLASSNAME = "org.apache.ranger.authorization.hadoop.RangerHdfsAuthorizer";

    private INodeAttributeProvider  rangerHdfsAuthorizerImpl;
    private RangerPluginClassLoader rangerPluginClassLoader;

    public RangerHdfsAuthorizer() {
        LOG.debug("==> RangerHdfsAuthorizer.RangerHdfsAuthorizer()");

        this.init();

        LOG.debug("<== RangerHdfsAuthorizer.RangerHdfsAuthorizer()");
    }

    public void init() {
        LOG.debug("==> RangerHdfsAuthorizer.init()");

        try {
            rangerPluginClassLoader = RangerPluginClassLoader.getInstance(RANGER_PLUGIN_TYPE, this.getClass());

            @SuppressWarnings("unchecked")
            Class<INodeAttributeProvider> cls = (Class<INodeAttributeProvider>) Class.forName(RANGER_HDFS_AUTHORIZER_IMPL_CLASSNAME, true, rangerPluginClassLoader);

            activatePluginClassLoader();

            rangerHdfsAuthorizerImpl = cls.newInstance();
        } catch (Exception e) {
            LOG.error("Error Enabling RangerHdfsPlugin", e);
        } finally {
            deactivatePluginClassLoader();
        }

        LOG.debug("<== RangerHdfsAuthorizer.init()");
    }

    @Override
    public void start() {
        LOG.debug("==> RangerHdfsAuthorizer.start()");

        rangerHdfsAuthorizerImpl.start();

        LOG.debug("<== RangerHdfsAuthorizer.start()");
    }

    @Override
    public void stop() {
        LOG.debug("==> RangerHdfsAuthorizer.stop()");

        rangerHdfsAuthorizerImpl.stop();

        LOG.debug("<== RangerHdfsAuthorizer.stop()");
    }

    @Override
    public INodeAttributes getAttributes(String fullPath, INodeAttributes inode) {
        return rangerHdfsAuthorizerImpl.getAttributes(fullPath, inode); // return default attributes
    }

    @Override
    public INodeAttributes getAttributes(String[] pathElements, INodeAttributes inode) {
        return rangerHdfsAuthorizerImpl.getAttributes(pathElements, inode);
    }

    @Override
    public AccessControlEnforcer getExternalAccessControlEnforcer(AccessControlEnforcer defaultEnforcer) {
        return rangerHdfsAuthorizerImpl.getExternalAccessControlEnforcer(defaultEnforcer);
    }

    private void activatePluginClassLoader() {
        if (rangerPluginClassLoader != null) {
            rangerPluginClassLoader.activate();
        }
    }

    private void deactivatePluginClassLoader() {
        if (rangerPluginClassLoader != null) {
            rangerPluginClassLoader.deactivate();
        }
    }
}
